// pages/category/index.js
let page = 1
Page({
  // 跳转到详情页（保留当前页面）

  
  /**
   * 页面的初始数据
   */
  data: {
    tabList:[],
    categoryList:[],
    filteredList:[],
    TabCur:0,
    active:0,
    checkIdx:0,
    srcollLeft:0,
    childTab:[]
  },
  handelTab(ev){
    let {idx} = ev.currentTarget.dataset
    let selectTab = this.data.tabList[idx]
    let {offsetLeft} = ev.currentTarget //当前被点击按钮，距离左侧的偏移量
    let {checkIdx,srcollLeft} = this.data
    if(idx>checkIdx){  //判断滑动方向，左滑
      srcollLeft = offsetLeft + 100
    }
    if(idx<checkIdx){ ////判断滑动方向，右滑
      srcollLeft = offsetLeft - 80
    }
    this.setData({checkIdx:idx})
     // 当前过滤条件
     const filteredData = this.filterFunc(this.data.categoryList);
    // let filteredData = [...this.data.categoryList]
    // if(idx == 1) {
    //   filteredData = filteredData.filter(item => item.sub == 101)
    // } else if(idx == 2) {
    //   filteredData = filteredData.filter(item => item.sub == 102)
    // } else if(idx == 3) {
    //   filteredData = filteredData.filter(item => item.sub == 103)
    // } else if(idx == 4) {
    //   filteredData = filteredData.filter(item => item.sub == 104)
    // }
    this.setData({
      srcollLeft,
      childTab:selectTab.child,
      filteredList:filteredData,
      active:0
    })
  },
  filterFunc(data) {
    const { checkIdx } = this.data;
    if (checkIdx === 0) {
      return data;
    } else {
      const subValue = 100 + checkIdx;
      console.log(subValue);
      return data.filter(item => item.sub === subValue);
    }
  },  
  // 子标签切换事件处理
  onChange(event) {
    this.setData({
      active: event.detail
    });
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 加载tab
    let res = await getApp().fly.get('/tabList')
    console.log(res);
    this.setData(
      {
        tabList:res.data,
        childTab:res.data[0].child
      })
    // 加载列表
    let {data} = await getApp().fly.get('/category',{
      _start:1,
      _limit:20
    })
    this.setData({categoryList:data,filteredList:data})
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom(){
    let start = page * 20
    console.log(page);
    let {data} = await getApp().fly.get('/category',{
      _start:start,
      _limit:20
    })
    page++
    console.log("data",data);
    if(data.length!=0){
      let list = this.data.categoryList.concat(data);
      console.log("list数据",list);
     
      this.setData({categoryList:list,filteredList:this.filterFunc(list)})
    }else{
      wx.showToast({
        title: '没有更多数据了',
        icon:"none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})